package com.cyzy.controller;

import com.cyzy.dto.Result;
import com.cyzy.pojo.AdminUser;
import com.cyzy.pojo.Menu;
import com.cyzy.pojo.RoleMenu;
import com.cyzy.pojo.RolePermission;
import com.cyzy.service.MenuService;
import com.cyzy.service.RolePermissionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/role")
public class RolePermissionController {
    //RESTful接口规范：查询用get  增加用post  修改用put  删除用delect
    //登录用post


    @Autowired
    private RolePermissionService rolePermissionService;

    @Autowired
    private MenuService menuService;

    //获取角色列表
    @GetMapping("/getrole")
    public Result getRolePermissionList(){
        List<RolePermission> rolePermissionList = rolePermissionService.selectRolePermissionList();
        if (rolePermissionList == null){
            return Result.error().message("获取角色列表失败！");
        }
        return Result.ok().data("rolePermissionList",rolePermissionList);
    }

    //获取菜单列表
    @GetMapping("/getmenu")
    public Result getMenuList(){
        List<Menu> menuList = menuService.selectAllMenuList();
        if (menuList == null){
            return Result.error().message("获取角色列表失败！");
        }
        return Result.ok().data("menuList",menuList);
    }

    //分页
    @GetMapping("/page")
    public Result roleMenuPageHelper(int page,int size){
        // 启动分页，page表示当前页，size表示每页的记录数
        PageHelper.startPage(page, size);

        // 获取分页后的数据
        List<RolePermission> rolePermissionList = rolePermissionService.selectRolePermissionList1();
        if (rolePermissionList == null || rolePermissionList.isEmpty()){
            return Result.error().message("暂无数据！");
        }

        PageInfo<RolePermission> pageInfo = new PageInfo<>(rolePermissionList);
//        System.out.println(adminMenuList);
        System.out.println(pageInfo + "2164835");
        // 返回分页后的数据列表
        return Result.ok().data("list",rolePermissionList).data("count",pageInfo.getTotal());
    }

    //搜索模块的查询
    @GetMapping("/searchrolemenu")
    public Result searchrolemenu(RolePermission rolePermission) {
        System.out.println(rolePermission + "1351351351");
        // 处理参数，防止 SQL 语句出错
        if ((rolePermission.getRole() == null || rolePermission.getRole().isEmpty()) &&
                (rolePermission.getMenuName() == null || rolePermission.getMenuName().isEmpty()) &&
                (rolePermission.getRolePermissionState() == null)) {
            return Result.error().message("请至少输入一个搜索条件");
        }

        List<RolePermission> rolePermissionList = rolePermissionService.searchworkersAll(rolePermission.getMenuName(),rolePermission.getRole(),rolePermission.getRolePermissionState());
//        System.out.println(rolePermissionList + "46846548");
        if (rolePermissionList != null && !rolePermissionList.isEmpty()) {
            PageInfo<RolePermission> pageInfo = new PageInfo<>(rolePermissionList);
            // 返回分页后的数据列表
            return Result.ok().data("list",rolePermissionList).data("count",pageInfo.getTotal());
        }

        return Result.error().message("未找到符合条件的管理员！");
    }

    //对角色列表进行添加
    @PostMapping("/add")
    public Result additionalManage(@RequestBody RolePermission rolePermission , @RequestParam("menuId") ArrayList<Integer> menuId){
        System.out.println(menuId + " " + "231843351481515");

//        //新增之前，判断该角色有无在数据库中存在
//        List<RolePermission> rolePermission1 = rolePermissionService.selectRolePermissionById(rolePermission.getRole());
//        System.out.println(rolePermission1 + "1684654865");
//        if (rolePermission1 != null && !rolePermission1.isEmpty()){
////            System.out.println("54648465");
//            return Result.error().message("该角色已存在，请勿重复添加！");
//        }
//
//        int res = rolePermissionService.getBaseMapper().insert(rolePermission);
//        System.out.println(res + "461548761846");
////        if (res == 0){
////            return Result.error().message("新增失败！");
////        }
        return Result.ok().message("新增成功！");
    }

    //对管理员进行修改
    @PutMapping("/upd")
    public Result updateManage(@RequestBody RolePermission rolePermission){
        System.out.println(rolePermission + " " + "262348513516848");

        int res = rolePermissionService.updateManage(rolePermission.getRoleId(),rolePermission.getRole(),rolePermission.getRolePermissionState(),rolePermission.getDescription());
//        System.out.println(res + " " + "4984684");
        if (res <= 0){
            return Result.error().message("修改失败！");
        }
        return Result.ok().message("修改成功!");
    }

    //对角色进行逻辑删除
    @DeleteMapping("/del")
    public Result deleteManage(@RequestParam("rolePermissionState") String state,@RequestParam("roleId") String Id){
//        System.out.println(state + " " + name + " " + "561535461");

        int res = rolePermissionService.deleteState(Id,state);

        if(res != 1){
            return Result.error().message("删除失败！");
        }
        return Result.ok().message("删除成功！");
    }

    //对角色进行逻辑批量删除
    @DeleteMapping("/dele")
    public Result deleteManage1(@RequestParam("rolePermissionState") String state,@RequestParam("roleId") List<String> ids){

        int res = rolePermissionService.deleteStateByIds(ids, state); // 调用批量逻辑删除方法

        if (res <= 0) {
            return Result.error().message("批量删除失败！");
        }
        return Result.ok().message("成功删除 " + res + " 条记录！");
    }
}
